package com.hjp.security.springboot.common;
import lombok.Data;
@Data
public class Response<T> {

    public static final String DEFAULT_SUCCESS_MESSAGE = "成功";

    public static final String DEFAULT_FAILED_MESSAGE = "失败";

    public static final String SUCCESS_CODE = "10000";

    public static final String FAILED_CODE = "99999";

    /**
     * 返回状态码
     */
    private String code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 请求成功失败
     */
    private boolean isSuccess;

    /**
     * 数据信息
     */
    private T data;

    private Response() {

    }

    public static <T> Response<T> builder() {
        return builder(null);
    }

    public static <T> Response<T> builder(T data) {
        Response<T> response = new Response<>();
        response.setSuccess(true);
        response.setCode(SUCCESS_CODE);
        response.setMessage(DEFAULT_SUCCESS_MESSAGE);
        response.setData(data);
        return response;
    }

    public static<T> Response<T> builderFailed(ResultCode resultCode){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setData(null);
        response.setCode(resultCode.getCode());
        response.setMessage(resultCode.getMessage());
        return response;
    }

    public static <T> Response<T> builderFailed() {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setData(null);
        response.setCode(FAILED_CODE);
        response.setMessage(DEFAULT_FAILED_MESSAGE);
        return response;
    }

    public static <T> Response<T> builderFailed(String msg) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setData(null);
        response.setCode(FAILED_CODE);
        response.setMessage(msg);
        return response;
    }

    public static <T> Response<T> builderFailed(CommonException e) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setData(null);
        response.setCode(e.getCode());
        response.setMessage(e.getMessage());
        return response;
    }

    public String getCode() {
        return this.code;
    }

    public Response setCode(String code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return this.message;
    }

    public Response setMessage(String message) {
        this.message = message;
        return this;
    }

    public boolean isSuccess() {
        return this.isSuccess;
    }

    public Response setSuccess(boolean success) {
        this.isSuccess = success;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public Response setData(T data) {
        this.data = data;
        return this;
    }

}
